package com.innoking.docsys.user.service.impl;

import com.innoking.docsys.user.dao.cluster.ClusterUpmsUserDao;
import com.innoking.docsys.user.dto.UpmsUser;
import com.innoking.docsys.user.module.UpmsUserVO;
import com.innoking.docsys.user.service.UserService;
import com.innoking.docsys.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
   private  ClusterUpmsUserDao clusterUpmsUserDao;
    @Override
    public UpmsUser getUserbyId(Integer id) {
        if(id==null){
            return  null;
        }
        UpmsUser user=clusterUpmsUserDao.selectByPrimaryKey( id );
        return user;
    }

    @Override
    public UpmsUser getUserbyAccount(String accountName) {

        UpmsUser user=clusterUpmsUserDao.selectByAccountName( accountName );
        return user;

    }

    /**
     * 用户登录验证
     * @param vo
     * @return
     */
    @Override
    public ResultObjVO checkUserLoginLegal(UpmsUserVO vo) {


        if(StringUtils.isEmpty(vo.getUsername())||StringUtils.isEmpty(vo.getPassword()) ){
           return ResultUtil.resData(null,"用户名或密码为空", StatusUtil.STATUS_ERROR);
        }
        UpmsUser user=clusterUpmsUserDao.selectByAccountName( vo.getUsername() );
        if(user==null){
            return ResultUtil.resData(null,"无此用户", StatusUtil.STATUS_ERROR);
        }
        String password= Md5Util.md5(vo.getPassword()+user.getSalt());

        if(password.equals(user.getPassword())){
            UpmsUserVO resVO= new UpmsUserVO();
            BeanUtils.copyProperties(user,resVO);
            resVO.setToken( JWTUtil.sign(user.getUserId(), 60L* 1000L* 30L));
            return ResultUtil.success(resVO);
        }else{
            return ResultUtil.resData(null,"密码错误", StatusUtil.STATUS_ERROR);
        }

    }
}